<template>
  <div class="tab-bar-item" @click="itemClick">
    <div v-if="!isActive">
      <slot name="item-icon"></slot>
    </div>
    <div v-else>
      <slot name="item-icon-active"></slot>
    </div>
    <div :style="activeStyle">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    // id:Number,
    // name:String,
    path: String,
    activeColor:{
      type:String,
      default:'red'
    }
  },
  data() {
    return {
      // isActive: true
    }
  },
  computed:{
    isActive(){
      //1.$route是一个跳转的路由对象（单个对象）,可以获取对应的name,path,params,query等,
      //2.this.$route.path是获取当前路由路径
      //3.indexOf方法，返回字符串在String对象中首次出现的字符位置
     return this.$route.path.indexOf(this.path)!=-1  //返回num类型
      //return this.$touer.path.includes(this.path)  //返回bool类型
    },
    activeStyle(){
      return this.isActive ? {color:this.activeColor}:{}
    }
  },
  methods: {
    itemClick() {
      //$touter路由全局对象
      this.$router.push(this.path).catch(err => {
        console.log("输出报错", err);
      });
    },
  }
};
</script>
<style>
.tab-bar-item {
  flex: 1;
  text-align: center;
  height: 49px;
  box-shadow: 0 -1px 1px rgba(100, 100, 100, 0.1);
  font-size: 14px;
}
.tab-bar-item img {
  width: 24px;
  height: 24px;
  margin-top: 3px;
  vertical-align: middle;
  margin-bottom: 2px;
}

</style>